<?php

//header guard
if(!class_exists('Vuelta')) {

//nasty oh die staan hier opeens -.-
function shas($n) { return isset($_SESSION[$n]); }
function sget($n) { return $_SESSION[$n]; }
function sset($n,$v) { $_SESSION[$n] = $v; }

function phas($n) { return isset($_POST[$n]); }
function pget($n) { return $_POST[$n]; }
function pempty() { return empty($_POST); }

function ghas($n) { return isset($_GET[$n]); }
function gget($n) { return $_GET[$n]; }
function gemtpy() { return empty($_GET); }
function is_logged_in() { return shas('id'); }
function ucname($string) {
  // converts a string to lowercase, then every first letter uppercase
  $string =ucwords(strtolower($string));

  //hacks, n stuff
  foreach (array('-', '\'') as $delimiter) {
    if (strpos($string, $delimiter)!==false) {
      $string =implode($delimiter, array_map('ucfirst', explode($delimiter, $string)));
    }
  }
  return $string;
}

class Vuelta {
  public static $home = 'vuelta/';
  public static $web_home;
  public static $sys_home;
  public static $url_home;
  public static $aliases = array();

  
  public static function init() {
    $root = $_SERVER['DOCUMENT_ROOT'];
    if($root != '/') $root .= '/';
    self::$sys_home = $root . self::$home;
    self::$web_home = '/' . self::$home;
    self::$url_home = $_SERVER['SERVER_NAME'] . self::$web_home;

    self::$aliases['site'] = 'site/';
    self::$aliases['util'] = 'util/';
    //self::$aliases['css'] = 'css/'; //the css file directory

    //session stuff
    session_start();

    self::load('site', 'site.php');
    self::load('util', 'DatabaseConnection.php');
  }
  

  public static function get_database_connection() {
    $db = new DatabaseConnection;
    $db->open('localhost', 'root', 'aardbei');
    //$db->open('mightydev.nl.mysql', 'mightydev_nl', 'aardbei');
    $db->select('mightydev_nl');
    return $db;
  }

  public static function web_path($dir, $file = NULL) {
    return self::$web_home . self::resolve($dir, $file);
  } 

  public static function sys_path($dir, $file = NULL) {
    return self::$sys_home . self::resolve($dir, $file);
  }

  public static function url_path($dir, $file = NULL) {
    return self::$url_home . self::resolve($dir, $file);
  }
  
  private static function resolve($dir, $file) {
    if(isset(self::$aliases[$dir])) return self::$aliases[$dir] . $file;
    return $dir . $file;
  }

  public static function load($dir, $file = NULL) {
    $path = self::sys_path($dir, $file);
    if(!(include $path)) {
?>    <p>Vuelta failed to include <?=$path?></p> <?php
    }
  }

  public static function css($dir, $file = NULL) {
    $path = self::web_path($dir, $file);
    if(file_exists(self::sys_path($dir, $file))) {
?>    <link rel="stylesheet" type="text/css" href="<?=$path?>"> <?php
    }
    else {
?>    <p>Vuelta failed to link css (<?=$path?>) because it doesn't exist</p> <?php
    }
  }
}

Vuelta::init();

};

?>
